package dbcontrol

import (
	"MqttCloudProtocolServer/publiclib/datamodel/dborm"

	uuid "github.com/satori/go.uuid"
)

//MqttconfControl struct
type MqttconfControl struct {
	base               dbOperabase
	Data               *dborm.Mqttconf
	ParentTCPConf      *ManageServerControl
	ChildrenUserServer []*UserserverControl
}

//CreateMqttconfControl 初始化几种指针函数
func CreateMqttconfControl() *MqttconfControl {
	content := new(MqttconfControl)
	content.base.GetData = func() interface{} {
		return content.Data
	}
	content.base.SetData = func(data interface{}) {
		if data != nil {
			content.Data = data.(*dborm.Mqttconf)
		}
	}
	content.base.GetKey = func() interface{} {
		if content.Data == nil {
			return nil
		}
		return content.Data.UUID
	}
	content.base.CreateNew = func() interface{} {
		res := new(dborm.Mqttconf)
		uuid := uuid.NewV4()
		res.UUID = uuid.String()
		return res
	}
	content.base.CreateNil = func() interface{} {
		return new(dborm.Mqttconf)
	}
	return content
}

//GetAll 获取全部用户-服务器映射信息
func (_this *MqttconfControl) GetAll() ([]*dborm.Mqttconf, error) {
	res := make([]*dborm.Mqttconf, 0)
	_, err := _this.base.GetAllByPage(&res, 0, 0, 0)
	return res, err
}

//GetAllByPage 获取全部用户-服务器映射信息.totPage<=0时，计算分页页数,返回值第二个参数为计算或传入的页数
func (_this *MqttconfControl) GetAllByPage(totPage, currentPage, countOfPage int) ([]*dborm.Mqttconf, int, error) {
	res := make([]*dborm.Mqttconf, 0)
	calTotpage, err := _this.base.GetAllByPage(&res, totPage, currentPage, countOfPage)
	return res, calTotpage, err
}

//CreateNew 创建一条记录
func (_this *MqttconfControl) CreateNew() {
	_this.Data = _this.base.CreateNew().(*dborm.Mqttconf)
}

//FindByID 根据主键获取一条记录
func (_this *MqttconfControl) FindByID(uuid string) error {
	return _this.base.FindByID(uuid)
}

//Save 更新当前条目
func (_this *MqttconfControl) Save() error {
	return _this.base.Save()
}

//Delete 删除当前条目
func (_this *MqttconfControl) Delete() error {
	return _this.base.Delete()
}

//GetByClientName 根据客户机名称查询
func (_this *MqttconfControl) GetByClientName(name string) ([]*dborm.Mqttconf, error) {
	res := make([]*dborm.Mqttconf, 0)
	err := _this.base.FindByWhere(&res, 0, 0, "client_name = ?", name)
	return res, err
}

//GetByTCPID 根据客户机名称查询
func (_this *MqttconfControl) GetByTCPID(tcpID string) ([]*dborm.Mqttconf, error) {
	res := make([]*dborm.Mqttconf, 0)
	err := _this.base.FindByWhere(&res, 0, 0, "master_id = ?", tcpID)
	return res, err
}

//GetParentTCPConf fun
func (_this *MqttconfControl) GetParentTCPConf() error {
	tcpServer := CreateTcpconfControl() // new(TcpconfControl)
	if err := tcpServer.FindByID(_this.Data.MasterID); err != nil {
		return err
	}
	_this.ParentTCPConf = tcpServer
	return nil
}

//GetChildrenUserServer 获取用户网关配对
func (_this *MqttconfControl) GetChildrenUserServer() error {
	userserver := CreateUserserverControl() // new(UserserverControl)
	res, err := userserver.GetByMQTTID(_this.Data.UUID)
	if err != nil || res == nil {
		return err
	}
	_this.ChildrenUserServer = make([]*UserserverControl, len(res))
	for i, item := range res {
		_this.ChildrenUserServer[i] = CreateUserserverControl() //new(UserserverControl)
		_this.ChildrenUserServer[i].Data = item
		_this.ChildrenUserServer[i].ParentMqttConf = _this
	}
	return nil
}
